import java.math.BigDecimal;
import java.util.List;

public class MathUtil {
    /**
     * 向上取整
     */
    public static double ceil(double value) {
        return Math.ceil(value - 0.00000001);
    }

    /**
     * 向下取整
     */
    public static double floor(double value) {
        return Math.floor(value + 0.00000001);
    }

    /**
     * sin(角度) = 数值
     */
    public static double sin(double angle) {
        return Math.sin(angle * Math.PI / 180);
    }

    /**
     * cos(角度) = 数值
     */
    public static double cos(double angle) {
        return Math.cos(angle * Math.PI / 180);
    }

    /**
     * tan(角度) = 数值
     */
    public static double tan(double angle) {
        return Math.tan(angle * Math.PI / 180);
    }

    /**
     * asin(数值) = 角度
     */
    public static double asin(double value) {
        return Math.asin(value) * 180 / Math.PI;
    }

    /**
     * acos(数值) = 角度
     */
    public static double acos(double value) {
        return Math.acos(value) * 180 / Math.PI;
    }

    /**
     * 获取平均数
     * @param values
     * @return
     */
    public static double avg(List<Double> values){
        Double sum=0d;
        for (Double v:values
        ) {
            sum=sum+v;
        }
        return sum/values.size();
    }

    /**
     * 保留小数位
     * @param value
     * @param decimalPlaces
     * @return
     */
    public static double round(double value, int decimalPlaces) {
        if (decimalPlaces < 0) {
            throw new IllegalArgumentException("小数位数必须是非负整数");
        }
        BigDecimal bd = new BigDecimal(Double.toString(value));
        bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }

    /**
     * 获取绝对值
     * @param value
     * @return
     */
    public static double abs(double value){
        return Math.abs(value);
    }

    /**
     * 保留小数点
     * @param value
     * @return
     */
    public static double changePoint(double value){
        String str = String.format("%.10f",value);
        return Double.parseDouble(str);
    }

    public static double twoNum(double value){
        String str = String.format("%.2f",value);
        return Double.parseDouble(str);
    }

    /**
     * 截取double整数部分
     * @param value
     * @return
     */
    public static double interceptInt(Double value){
        return Double.valueOf(value.longValue());
    }

    public static double roundToThreeDecimalPlaces(double number) {
        return Math.round(number * 1000) / 1000.0;
    }
}
